﻿using Microsoft.Practices.WizardFramework;
using Sixeyed.CodeGeneration.Metadata;
using Sixeyed.CodeGeneration.UI.Enums;
using System;
using System.Collections.Generic;

namespace Sixeyed.Guidance.CodeGeneration.CustomWizardPages
{
    /// <summary>
    /// Wizard page for selecting metadata source and provider used in code generation
    /// </summary>
    public partial class SelectSource : CodeGenerationPageBase
    {
        /// <summary>
        /// Gets/sets configuration of the metadata source
        /// </summary>
        [RecipeArgument]
        public SourceConfiguration SourceConfiguration
        {
            get { return this.GetArgumentValue<SourceConfiguration>("SourceConfiguration"); }
            set
            {
                if (value == null)
                {
                    value = new SourceConfiguration();
                }
                this.SetArgumentValue<SourceConfiguration>("SourceConfiguration", value);
            }
        }

        /// <summary>
        /// Constructor with initial state
        /// </summary>
        /// <param name="parent">Parent wizard form</param>
        public SelectSource(WizardForm parent) : base(parent)
        {
            InitializeComponent();
            this.sourceSelection1.SourceSelectionChanged += new Sixeyed.CodeGeneration.UI.SourceSelection.SourceSelectionChangedHandler(sourceSelection1_SourceSelectionChanged);
        }

        /// <summary>
        /// Returns true if metadata source is fully configured
        /// </summary>
        public override bool IsDataValid
        {
            get
            {
                bool isValid = true;
                try
                {
                    List<SourceConfiguration.Arguments> validArguments = new List<SourceConfiguration.Arguments>(1);
                    validArguments.Add(SourceConfiguration.Arguments.ProviderNameAndAbove);
                    this.SourceConfiguration.Validate(validArguments);
                }
                catch (ArgumentException ex)
                {
                    isValid = false;
                    this.InfoRTBoxText = ex.Message;
                }
                return isValid;
            }
        }

        private void sourceSelection1_SourceSelectionChanged(object sender, SourceSelectionType changeType)
        {
            switch (changeType)
            {
                case SourceSelectionType.SourceType:
                    this.SourceConfiguration.SourceTypeName = this.sourceSelection1.SelectedSourceType;
                    break;
                case SourceSelectionType.Source:
                    this.SourceConfiguration.SourceName = this.sourceSelection1.SelectedSource.Name;
                    break;
                case SourceSelectionType.SourceUri:
                    this.SourceConfiguration.SourceUri = this.sourceSelection1.SelectedSource.Uri;
                    //update the provider to reset the Uri:
                    if (this.sourceSelection1.SelectedProvider != null)
                    {
                        Metadata.Provider = this.sourceSelection1.SelectedProvider;
                    }
                    break;
                case SourceSelectionType.Provider:
                    this.SourceConfiguration.ProviderName = this.sourceSelection1.SelectedProvider.Name;
                    Metadata.Provider = this.sourceSelection1.SelectedProvider;
                    break;
            }
            this.EnableNextStep();
        }
    }
}